package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsPipelineField;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPipelineFieldDao.Properties;
import com.gesee.zyspps.utils.StringUtil;
import com.gesee.zyspps.utils.ToastUtil;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class UpdateZiduanActivity extends BaseActivity
{

    private Spinner xianshi, data_type, data_length, address, notNull;
    private String xianshiValue,dataTypeValue,dataLengthValue,addressValue,notNullValue;
    private EditText filedName, xiaoshu, remark,et_data_length;
    private Button enter, cancle;
    private CmsPipelineField oldCms;
    private ImageView come_back;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_ziduan);
        oldCms = (CmsPipelineField) getIntent().getSerializableExtra("CmsPipelineField");
        initView();
    }

    private void initView()
    {
        // TODO Auto-generated method stub
        come_back = (ImageView) findViewById(R.id.come_back);
        come_back.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateZiduanActivity.this.finish();
            }
        });
        
        filedName = (EditText) findViewById(R.id.name);
        filedName.setText(oldCms.getFiledName().trim());
        //xiaoshu = (EditText) findViewById(R.id.xiaoshu);
        remark = (EditText) findViewById(R.id.remark);
        remark.setText(oldCms.getRemark().trim());
        et_data_length = (EditText) findViewById(R.id.et_data_length);
        et_data_length.setText(oldCms.getDataLength().trim());
        
        xianshiValue = oldCms.getShowFiled().trim();
        dataTypeValue = oldCms.getDataType().trim();
        dataLengthValue = oldCms.getDataLength().trim();
        addressValue = oldCms.getLocation().trim();
        notNullValue = oldCms.getNotNull().trim();
        
        enter = (Button) findViewById(R.id.enter);
        cancle = (Button) findViewById(R.id.cancle);
        
        xianshi = (Spinner) findViewById(R.id.xianshi);
        List xianshiList = new ArrayList();
        xianshiList.add("ALL");
        ArrayAdapter xianshiAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, xianshiList);
        xianshiAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        xianshi.setAdapter(xianshiAdapter);
        
        xianshi.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                xianshiValue = "ALL";
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        if(oldCms.getShowFiled().equals("ALL")){
            xianshi.setSelection(0);
        }
        

        data_type = (Spinner) findViewById(R.id.data_type);
        List data_typeList = new ArrayList();
        data_typeList.add("长整型");
        data_typeList.add("整型");
        data_typeList.add("文本");
        data_typeList.add("单精度型");
        data_typeList.add("双精度型");
        data_typeList.add("日期型");
        data_typeList.add("布尔");
        ArrayAdapter data_typeAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, data_typeList);
        data_typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        data_type.setAdapter(data_typeAdapter);
        
        data_type.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                if(position == 0){
                    dataTypeValue="长整型";
                }
                if(position == 1){
                    dataTypeValue="整型";
                }
                if(position == 2){
                    dataTypeValue="文本";
                }
                if(position == 3){
                    dataTypeValue="单精度型";
                }
                if(position == 4){
                    dataTypeValue="双精度型";
                }
                if(position == 5){
                    dataTypeValue="日期型";
                }
                if(position == 5){
                    dataTypeValue="布尔";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        if(oldCms.getDataType().equals("长整型")){
            data_type.setSelection(0);
        }
        if(oldCms.getDataType().equals("整型")){
            data_type.setSelection(1);
        }
        if(oldCms.getDataType().equals("文本")){
            data_type.setSelection(2);
        }
        if(oldCms.getDataType().equals("单精度型")){
            data_type.setSelection(3);
        }
        if(oldCms.getDataType().equals("双精度型")){
            data_type.setSelection(4);
        }
        if(oldCms.getDataType().equals("日期型")){
            data_type.setSelection(5);
        }
        if(oldCms.getDataType().equals("布尔")){
            data_type.setSelection(6);
        }

        data_length = (Spinner) findViewById(R.id.data_length);
        List data_lengthList = new ArrayList();
        data_lengthList.add("20");
        ArrayAdapter data_lengthAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, data_lengthList);
        data_lengthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        data_length.setAdapter(data_lengthAdapter);
        
        data_length.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                et_data_length.setText("20");
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        if(oldCms.getDataLength().equals("20")){
            data_length.setSelection(0);
        }

        address = (Spinner) findViewById(R.id.address);
        List addressList = new ArrayList();
        addressList.add("点库");
        addressList.add("线库");
        addressList.add("测量库");
        ArrayAdapter addressAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, addressList);
        addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        address.setAdapter(addressAdapter);
        address.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                if(position == 0){
                    addressValue = "点库";
                }
                if(position == 1){
                    addressValue = "线库";
                }
                if(position == 2){
                    addressValue = "测量库";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        if(oldCms.getLocation().equals("点库")){
            address.setSelection(0);
        }
        if(oldCms.getLocation().equals("线库")){
            address.setSelection(1);
        }
        if(oldCms.getLocation().equals("测量库")){
            address.setSelection(2);
        }

        /*notNull = (Spinner) findViewById(R.id.notNull);
        List notNullList = new ArrayList();
        notNullList.add("是");
        notNullList.add("否");
        ArrayAdapter notNullAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, notNullList);
        notNull.setAdapter(notNullAdapter);
        
        notNull.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                if(position == 0){
                    addressValue = "是";
                }
                if(position == 1){
                    addressValue = "否";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        if(oldCms.getNotNull().equals("是")){
            notNull.setSelection(0);
        }
        if(oldCms.getLocation().equals("否")){
            notNull.setSelection(1);
        }*/
        
        enter.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String filedNameValue = filedName.getText().toString().trim();
                String xiaoshuValue = xiaoshu.getText().toString().trim();
                String remarkValue = remark.getText().toString().trim();
                dataLengthValue = et_data_length.getText().toString().trim();
                if(StringUtil.isEmpty(filedNameValue)){
                    ToastUtil.show(UpdateZiduanActivity.this, "字段名称必须填写!");
                }else{
                    oldCms.setDataLength(dataLengthValue);
                    oldCms.setDataType(dataTypeValue);
                    oldCms.setFiledName(filedNameValue);
                    oldCms.setLocation(addressValue);
                    oldCms.setNotNull(notNullValue);
                    oldCms.setRemark(remarkValue);
                    oldCms.setShowFiled(xianshiValue);
                    ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().update(oldCms);
                    UpdateZiduanActivity.this.finish();
                }
            }
        });
        
        cancle.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateZiduanActivity.this.finish();
            }
        });

    }
}
